package gui;

import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.JTextField;

@SuppressWarnings("serial")
public class HistoryTextField extends JTextField implements KeyListener {
	private ArrayList<String> history;
	private int timeMachine;
	
	public HistoryTextField(int size) {
		super(size);
		history = new ArrayList<String>();
		timeMachine = 0;
		addKeyListener(this);
	}

	public void archiveAndClear() {
		history.add(getText());
		timeMachine = history.size();
		setText("");
	}

	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
		
		case KeyEvent.VK_UP: case KeyEvent.VK_KP_UP:
			if (timeMachine > 0) {
				timeMachine -= 1;
				setText(history.get(timeMachine));
			}
			break;
			
		case KeyEvent.VK_DOWN: case KeyEvent.VK_KP_DOWN:
			if (timeMachine < history.size() - 1) {
				timeMachine += 1;
				setText(history.get(timeMachine));
			}
			break;
		}
	}

	public void keyReleased(KeyEvent e) {}

	public void keyTyped(KeyEvent e) {}
}
